package com.ivi.car.module.reservation.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ivi.car.R;
import com.ivi.car.base.BaseActivity;
import com.ivi.car.bean.ReservationData;
import com.ivi.car.module.store.view.StoreActivity;
import com.ivi.car.module.timeselection.view.activity.TimeSelectionActivity;
import com.ivi.car.module.reservation.adapter.ReservationAdapter;
import com.ivi.car.module.reservation.adapter.ReservationMultitypeItemAdapter;
import com.ivi.car.module.reservation.model.ReservationContract;
import com.ivi.car.module.reservation.prensenter.ReservationPrensenter;
import com.wm.machine.baselibrary.common.widget.recyclerview.itemdecoration.SpaceItemDecoration;
import com.wm.machine.baselibrary.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Copyright
 * author kevin
 * date 2018/3/6
 * describe 预定保养页面
 * history
 */
public class ReservationActivity extends BaseActivity implements ReservationContract.View {
  private static final int ITEM_POSITION_TWO = 2;
  @BindView(R.id.reservation_back_imgVi)
  ImageView reservationBackimgVi;
  @BindView(R.id.reservation_recycler_view)
  RecyclerView reservationRecyclerview;
  @BindView(R.id.reservation_btn1)
  TextView reservationBtn1;
  @BindView(R.id.reservation_success_relativelayout)
  RelativeLayout reservationSuccessRelativelayout;
  private ReservationAdapter mReservationAdapter;
  private ReservationMultitypeItemAdapter multitypeItemAdapter;
  private List<ReservationData> datas;
  private List<ReservationData> datas2;
  private ReservationPrensenter mReservationPrensenter;

  public static final int FROMXDELTA = 2000;
  public static final int ANIMATIONDURATION = 50;
  public static final int TOPBOTTOM = 15;
  // 测试常量（接入数据后删除）
  public static final int TEXT1 = 15;
  public static final int TEXT2 = 8;
  public static final int TEXT3 = 9;
  public static final int TEXT4 = 5;
  public static final int TEXT5 = 6;

  @Override
  protected int getLayoutId() {
    return R.layout.activity_reservation;
  }

  @Override
  protected void initView() {
    // reservationBtn1.setOnClickListener(new View.OnClickListener() {
    // @Override
    // public void onClick(View v) {
    // reservationSuccessRelativelayout.setVisibility(View.VISIBLE);
    // }
    // });
  }

  /**
   * 点击事件处理
   *
   * @param view 点击事件对应的view
   */
  @OnClick({R.id.reservation_btn1, R.id.reservation_back_imgVi,
      R.id.reservation_success_relativelayout_btn1,
      R.id.reservation_success_relativelayout_btn2})
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.reservation_btn1:
        mReservationPrensenter.loadReservationSuccessData("success");
        /**
         * 以下代码放入view层回调
         */
        TranslateAnimation ta = new TranslateAnimation(FROMXDELTA, 0, 0, 0);
        ta.setDuration(ANIMATIONDURATION);
        reservationSuccessRelativelayout.startAnimation(ta);
        reservationSuccessRelativelayout.setVisibility(View.VISIBLE);
        break;
      case R.id.reservation_back_imgVi:
        finish();
        break;
      case R.id.reservation_success_relativelayout_btn1:
        finish();
        break;
      case R.id.reservation_success_relativelayout_btn2:
        finish();
        ToastUtils.showShortControl(this, "进入个人中心");
        break;
      default:
        break;
    }
  }

  @Override
  protected void initData() {
    mReservationPrensenter = new ReservationPrensenter(this, mLifecycleProvider);
    /**
     * 获取页面数据
     */
    mReservationPrensenter.loadReservationData("123456789");
  }

  @Override
  public void onPointerCaptureChanged(boolean hasCapture) {

  }

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /**
     * 以下方法放入onReservationDataSuccess回调解析数据
     */
    datas = new ArrayList<>();
    datas2 = new ArrayList<>();
    ReservationData model;
    for (int i = 0; i < TEXT1; i++) {
      model = new ReservationData();
      model.setNum(i);
      if (i < TEXT2) {
        model.setItemType(i);
      } else if (i == TEXT3) {
        model.setItemType(TEXT4);
      } else {
        model.setItemType(TEXT5);
      }
      // model.setItemType(i);
      datas.add(model);
    }
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    reservationRecyclerview.setLayoutManager(layoutManager);
    reservationRecyclerview.addItemDecoration(new SpaceItemDecoration(0, TOPBOTTOM));
    multitypeItemAdapter = new ReservationMultitypeItemAdapter(datas);
    mReservationAdapter = new ReservationAdapter(R.layout.reservation_item_store, datas);
    multitypeItemAdapter
        .setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
          @Override
          public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            TextView txtVi1 = (TextView) view;
            Toast.makeText(ReservationActivity.this, "" + txtVi1.getText(), Toast.LENGTH_SHORT)
                .show();
          }
        });
    multitypeItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
      @Override
      public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ToastUtils.showShortControl(ReservationActivity.this, "点击：" + position);
        if (position == 0) {
          startActivity(new Intent(ReservationActivity.this, StoreActivity.class));
        } else if (position == ITEM_POSITION_TWO) {
          startActivity(new Intent(ReservationActivity.this, TimeSelectionActivity.class));
        }
      }
    });
    reservationRecyclerview.setAdapter(multitypeItemAdapter);
  }

  @Override
  public void onReservationSuccessDataSuccess(String msg) {
    /**
     * 执行操作
     * 成功弹出预约成功画面 （失败页面待定）
     */
  }

  @Override
  public void onReservationDataSuccess(String msg) {
    /**
     * 执行操作页面加载操作
     */
  }
}
